//AUTHOR:  Aaron Kaufman
// Latest Edit:  12/2/2012

/*
 * This class is responsible for changing which role the user is currently viewing,
 * as well as displaying the current role's relevant panels.
 */

package gui;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import model.Author;
import model.Conference;
import model.ProgramChair;
import model.Reviewer;
import model.SubProgramChair;

@SuppressWarnings("serial")
public class WorkspaceViewPanel extends JPanel
{								
	private JPanel my_combo_box_pane;
	//Thin bit at the top controlling the current view.
	private ViewBoxPanel my_view_box;
	//The current role's view!
	private JButton my_back_button;

	public final static Dimension SUB_PANEL_DIMENSION = new Dimension(1000,500);
    
    
    private Map<String, Component> my_name_to_cards;
    

	public WorkspaceViewPanel(JButton the_view_change_button, String the_user, Conference the_conf)
	{
		super();
		my_back_button = the_view_change_button;
		my_view_box = new ViewBoxPanel();
		my_combo_box_pane = new JPanel();
		my_name_to_cards = new HashMap<String, Component>();
		my_combo_box_pane.setOpaque(false);
		my_view_box.setOpaque(false);
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBackground(BaseViewFrame.BACKGROUND_COLOR);
		
		ProgramChair prog_chair = the_conf.getProgramChair();
		SubProgramChair sub_chair = the_conf.getProgramChair().getSubprogramChairs().get(the_user);
		Reviewer rev = prog_chair.getReviewers().get(the_user);
		Author auth = the_conf.getAuthorRole(the_user);

		if (the_conf.getProgChairUserName().equals(the_user))
		{ 
		    
		    my_name_to_cards.put("Program Chair", createProgChairPanel(prog_chair));
		    
		}
		if (sub_chair != null)
		{
		    my_name_to_cards.put("Subprogram Chair", createSubChairPanel(sub_chair, prog_chair, the_user));
		}
		if (rev != null)
		{
		    my_name_to_cards.put("Reviewer", createReviewerPanel(rev));
		}
		if (auth != null)
		{
		    my_name_to_cards.put("Author", createAuthorPanel(auth, the_conf));
		}

		my_name_to_cards.put("Submit New Manuscript", constructManuscriptSubmitter(the_user, the_conf));
		
		JPanel top_panel = new JPanel(new FlowLayout());
		top_panel.setOpaque(false);
		JComboBox cb = new JComboBox(my_name_to_cards.keySet().toArray());
		cb.setEditable(false);
        cb.addItemListener(my_view_box);
        my_combo_box_pane.add(cb);
        top_panel.add(my_back_button);
        top_panel.add(my_combo_box_pane);
        this.add(top_panel);
        this.add(my_view_box);
        
        for (String e: my_name_to_cards.keySet())
        {
            my_view_box.add(e, my_name_to_cards.get(e));
            
            
        }
		
	}
	


	
	private JPanel createSubChairPanel(SubProgramChair the_sub_chair, ProgramChair the_prog_chair, String the_user)
    {
        return new SPCViewport(the_sub_chair, the_prog_chair, the_user);
    }




    private Component createProgChairPanel(ProgramChair the_prog_chair)
    {
        return new JScrollPane(new ProgramChairViewPanel(the_prog_chair));
        
    }




    private JPanel createReviewerPanel(Reviewer the_reviewer)
    {
        return new ReviewerViewPanel(the_reviewer);
    }




    private JPanel constructManuscriptSubmitter(String the_user, Conference the_conf)
    {
        return new AuthorPanelSubmit(the_user, the_conf);
    }




    private JPanel createAuthorPanel(Author the_author, Conference the_conf)
    {
        
        return new AuthorViewPanel(the_author, the_conf);
    }



    //Creates the panel that shows the actual display.
    private class ViewBoxPanel extends JPanel implements ItemListener{

        public ViewBoxPanel()
        {
            super(new CardLayout());
        }
        
        @Override
        public void itemStateChanged(ItemEvent the_dropdown)
        {
            CardLayout cl = (CardLayout)(my_view_box.getLayout());
            cl.show(my_view_box, (String)the_dropdown.getItem());
            
        }
        
    }
}
